﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VASJ.Model;

namespace VASJ.Service.Library.Manager
{
    public class RequestOrganizationBeOwnerManager : BaseManager
    {
         /// <summary>
        /// Init
        /// </summary>
        /// <param name="_ApplicationName"></param>
        public RequestOrganizationBeOwnerManager(string _ApplicationName)
            : base(_ApplicationName)
        {
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Set Org be owner User
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="orgId"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public int SetOrganBeOwnerUser(Guid userid, Guid orgId, ref string msgError)
        {
            try
            {
                var _user = this.mdb.AHSAspnetMemberships.Where(m => m.Userid == userid).FirstOrDefault();

                _user.Ownerid = orgId;
                _user.Ownerisuser = false;
                this.mdb.SubmitChanges();

                var _org = (from o in this.mdb.AHSRequestOrganizationBeOwners
                            join oip in this.mdb.AHSOrganizationInProjects on o.OrgID equals oip.Orgid 
                            where (o.UserID == userid) && (o.OrgID == orgId) && 
                                (oip.AHSMainProject.AHSProjectType.ApplicationName.ToLower() == this.ApplicationName.ToLower())
                            select o).FirstOrDefault();

                if (_org != null)
                {
                    this.mdb.AHSRequestOrganizationBeOwners.DeleteOnSubmit(_org);
                    this.mdb.SubmitChanges();
                }

                return 0;
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + this.ApplicationName + "] RequestOrganizationBeOwnerManager.SetOrganBeOwnerUser error {0}", ex);
                msgError = ex.Message;

                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Set Org be owner user and Assign user to org
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="orgId"></param>
        /// <returns></returns>
        public int SetOrganBeOwnerUserAndAssignUserToOrg(Guid userid, Guid orgId, ref string msgError)
        {
            try
            {
                //var _user = this.mdb.AHSAspnetMemberships.Where(m => m.Userid == userid).FirstOrDefault();
                var _user = (from u in this.mdb.AHSAspnetMemberships
                             join uip in this.mdb.AHSUserInProjects on u.Userid equals uip.Userid
                             where (u.Userid == userid) && 
                                   (uip.AHSMainProject.AHSProjectType.ApplicationName.ToLower() == this.ApplicationName.ToLower())
                             select u).FirstOrDefault();

                _user.Ownerid = orgId;
                _user.Ownerisuser = false;

                var assign = (from u in this.mdb.AHSUsersInOrganizations
                              join oip in this.mdb.AHSOrganizationInProjects on u.OrgId equals oip.Orgid 
                              where (u.UserId == userid) && (u.OrgId == orgId) &&
                                    (oip.AHSMainProject.AHSProjectType.ApplicationName.ToLower() == this.ApplicationName.ToLower())
                              select u).FirstOrDefault();
                
                if (assign == null)
                {
                    //Not exist

                    assign = new AHSDbContext.AHSUsersInOrganization();
                    assign.UserId = userid;
                    assign.OrgId = orgId;
                    assign.IsInvite = false;
                    assign.Status = 1;
                    this.mdb.AHSUsersInOrganizations.InsertOnSubmit(assign);
                }
                else
                {
                    //Exist
                    assign.Status = 1;
                }

                var _delRq = (from rq in this.mdb.AHSRequestOrganizationBeOwners
                             join oip in this.mdb.AHSOrganizationInProjects on rq.OrgID equals(oip.Orgid) 
                             where (rq.UserID == userid) && (rq.OrgID == orgId) && 
                                   (oip.AHSMainProject.AHSProjectType.ApplicationName.ToLower() == this.ApplicationName.ToLower())
                              select rq).FirstOrDefault();

                if (_delRq != null) this.mdb.AHSRequestOrganizationBeOwners.DeleteOnSubmit(_delRq);

                this.mdb.SubmitChanges();

                return 0;
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + this.ApplicationName + "] RequestOrganizationBeOwnerManager.SetOrganBeOwnerUserAndAssignUserToOrg error {0}", ex);
                msgError = ex.Message;

                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Remove request be owner
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public int RemoveRequestBeOwner(int Id, ref string msgError)
        {
            try
            {
                var delete = (from rqdb in this.mdb.AHSRequestOrganizationBeOwners 
                              join oip in this.mdb.AHSOrganizationInProjects on rqdb.OrgID equals(oip.Orgid)
                              where (rqdb.Id == Id) && (oip.AHSMainProject.AHSProjectType.ApplicationName.ToLower() == this.ApplicationName.ToLower()) 
                              select rqdb).FirstOrDefault();

                this.mdb.AHSRequestOrganizationBeOwners.DeleteOnSubmit(delete);

                this.mdb.SubmitChanges();
                return 0;
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + this.ApplicationName + "] RequestOrganizationBeOwnerManager.RemoveRequestBeOwner error {0}", ex);
                msgError = ex.Message;

                return -1;
            }
        }
    }
}